<?php

namespace app\api\controller;
use app\api\common\Common;
use app\api\common\ApiErrDesc;

class Shopping extends Common {

    /**
     * 购物车列表
     * @return [Json]   [返回当前用户购物车列表信息]
     */
    public function show(){
        $data = $this->params;
        $dataurl = $this->__DATAURL__;
        $shopping   = db("shopping_char");
        //$shangchang = db("shangchang");
        $product    = db("product");
        
        $cart = $shopping->where('uid='.intval($data['uid']))->field('id, uid, pid, price, num')->select();
        foreach ($cart as $k => $v) {
        	$pro_info = $product->where('id='.intval($v['pid']))->field('name, photo_x')->find();
        	$cart[$k]['pro_name'] = $pro_info['name'];
        	$cart[$k]['photo_x']  = $dataurl.$pro_info['photo_x'];
        }

		$this->return_msg(ApiErrDesc::SUCCESS[0], ApiErrDesc::SUCCESS[1], $cart);
    }

    /**
     * 添加到购物车
     * @return [json]    [添加到购物车结果]
     */
    // 
    public function add() {
        $data = $this->params;
        // 检查商品是否存在或者删除
        $check = db('product')->where('id='.$data['pid'].' AND del = 0 AND is_down = 0')->find();
        if(!$check){
            $this->return_msg(ApiErrDesc::PRODUCTSDOWN[0], ApiErrDesc::PRODUCTSDOWN[1]);
        }

        // 查看商品及判断库存容量
        $check_info = db('product')->where('id='.$data['pid'].' AND del=0 AND is_down=0')->find();
        if(intval($check_info['num'])<=$data['num']){
            $this->return_msg(ApiErrDesc::PRODUCTNONE[0], ApiErrDesc::PRODUCTNONE[1]);
        }

        // 判断购物车内是否已经存在该商品
        $shpp = db('shopping_char');
        $cartData = array();
        $cart_info = $shpp->where('pid='.intval($data['pid']).' AND uid='.intval($data['uid']))->field('id,num')->find();
		if ($cart_info) {
			$cartData['num'] = intval($cart_info['num'])+intval($data['num']);
			//判断库存
			if (intval($check_info['num'])<=$cartData['num']) {
				$this->return_msg(ApiErrDesc::PRODUCTNONE[0], ApiErrDesc::PRODUCTNONE[1]);
			}
			$res = $shpp->where('id='.intval($cart_info['id']))->update($cartData);
		}else{
			$cartData['pid']=intval($data['pid']);
			$cartData['num']=intval($data['num']);
			$cartData['addtime']=time();
			$cartData['uid']=intval($data['uid']);
			$cartData['shop_id']=intval($check_info['shop_id']);
			$ptype = 1;
			if (intval($check_info['pro_type'])) {
				$ptype = intval($check_info['pro_type']);
			}
			$cartData['type']=$ptype;
			$cartData['price'] = $check_info['price_yh'];

            $res=$shpp->insert($cartData);
        }
        
        if($res){
            $this->return_msg(ApiErrDesc::SUCCESS[0], ApiErrDesc::SUCCESS[1]);
        } else {
            // 操作失败
            $this->return_msg(ApiErrDesc::ERROR[0], ApiErrDesc::ERROR[1]);
        }
    }

    /**
     * 删除购物车商品 [可批量删除]
     * @return [json]    [删除结果]
     */
    public function delete(){
        $data = $this->params;
        $shopping = db("shopping_char");
		/*$check_id = $shopping->where('id in ('.trim($data['cart_id'], ','))->find();
		if (!$check_id) {
			$this->return_msg(ApiErrDesc::SHOPPINGNONE[0], ApiErrDesc::SHOPPINGNONE[1]);
		}*/

        // 删除
	    $res = $shopping->where('id in ('.implode("," , $data['cart_id']).') AND uid='.$data['uid'])->delete();
		if($res){
			$this->return_msg(ApiErrDesc::SUCCESS[0], ApiErrDesc::SUCCESS[1]);
		}else{
			$this->return_msg(ApiErrDesc::ERROR[0], ApiErrDesc::ERROR[1]);
		}
    }

    /**
     * 修改购物车数量接口
     * @return [json]    [修改结果]
     */
    public function up_cart(){
        $data = $this->params;
		$shopping = db("shopping_char");

		$check = $shopping->where('id='.intval($data['cart_id']))->find();
		if (!$check) {
			$this->return_msg(ApiErrDesc::SHOPPINGNONE[0], ApiErrDesc::SHOPPINGNONE[1]);
		}

		//检测库存
		$pro_num = db('product')->where('id='.intval($check['pid']))->column('num');
		if($data['num'] > intval($pro_num)){
			$this->return_msg(ApiErrDesc::PRODUCTNONE[0], ApiErrDesc::PRODUCTNONE[1]);
		}
		
		$dataCart = array();
		$dataCart['num'] = $data['num'];

		$res = $shopping->where('id ='.intval($data['cart_id']).' AND uid='.intval($data['uid']))->update($dataCart);
		if ($res) {
			$this->return_msg(ApiErrDesc::SUCCESS[0], ApiErrDesc::SUCCESS[1]);
		}else{
			$this->return_msg(ApiErrDesc::ERROR[0], ApiErrDesc::ERROR[1]);
		}
    }
    

    /**
     * 点击购物车中的立即购买下单
     * @return [json]    返回订单信息
     */
    public function check_shop(){
        $data = $this->params;
        $id = $data['cart_id'];
        foreach($id as $k=>$v){
            $shoop[$k] = db("shopping_char")->where('id ='.$v)->field('shop_id,pid')->find();
        }

        foreach($shoop as $key => $value){
			$result[$key] = db("product")->where('id='.$value['pid'])->field('id,price,price_yh')->select();
			$price[] = $this->i_array_column($result[$key], 'price_yh');
        }

        $str = '';
        foreach($price as $keys => $va){
			$str .= implode(",", $va).",";
		}
		$str = trim($str, ",");
        $parr = explode(",", $str);
        if(array_sum($parr) && in_array("0", $parr)){
			$this->return_msg(ApiErrDesc::ZONEORDER[0], ApiErrDesc::ZONEORDER[1]);
        }
        $names = $this->i_array_column($shoop, 'shop_id');
		$arr = array_unique($names);
        $val = sizeof($arr);
        
        // 现今商品的商家只有1家
        if ($val) {
			$this->return_msg(ApiErrDesc::SUCCESS[0], ApiErrDesc::SUCCESS[1]);
		}else{
			$this->return_msg(ApiErrDesc::ERROR[0], ApiErrDesc::ERROR[1]);
		}
    }
}